package com.medical_houduan.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.medical_houduan.commons.Result;
import com.medical_houduan.dto.CityDto;
import com.medical_houduan.mapper.CityMapper;
import com.medical_houduan.pojo.City;
import com.medical_houduan.service.CityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CityServiceImpl implements CityService {
    @Autowired
    CityMapper cityMapper;

    //查询所有城市信息
    @Override
    public Result getCityInfoService(String key,Integer currentPage,Integer pageSize) {

        //启动分页操作，并自动干扰下一个SQL查询（添加limit子句）
        PageHelper.startPage(currentPage, pageSize);
        //sql查询语句已经动态拼接关键字，直接调用即可，无须在此判断关键字，当关键字为空,会查询所有城市信息
        List<CityDto> list = cityMapper.getCityInfos(key);
        //将分页查询到的数据放入PageInfo，拥有pages,total,list，pageNum,pageSize等属性
        PageInfo<CityDto> pg = new PageInfo<>(list,5);
        if (pg.getList().isEmpty()){
            return new Result(201,"没有更多数据了",pg);
        }
        return new Result(200,"已获取√",pg);
    }

    //添加城市
    @Override
    public Result addCityService(CityDto cityDto) {
        //判断传入的参数
        if(cityDto==null){
            return  new Result(201,"城市信息必填");
        }
        if(cityDto.getCityNumber()==null){
            return  new Result(202,"城市编号必填");
        }
        //cityId对应一个CityNumber,根据CityNumber判断城市是否存在
        if(cityMapper.exists(new QueryWrapper<City>().eq("city_number",cityDto.getCityNumber()))){
            return  new Result(203,"该城市信息已存在");
        }
        City city = new City();
        BeanUtils.copyProperties(cityDto,city);//对象拷贝
        city.setCreateTime(new Date());
        city.setUpdateTime(new Date());
        int row= cityMapper.insert(city);
        if(row!=1){
            return new Result(203,"操作失败，稍后重试");
        }
        return new Result(200,"已添加√");
    }

    //删除城市
    @Override
    public Result deleteCityService(Long cityId) {
        if(cityId==null){
            return new Result(201,"指定的城市数据为空");
        }
        //判断数据库是否存在该城市
        if(!(cityMapper.exists(new QueryWrapper<City>().eq("city_id",cityId)))){
            return new Result(202,"指定删除的城市不存在");
        }
        //先删除该城市下的医保政策
        //再删除该城市
        int row= cityMapper.deleteById(cityId);
        if (row!=1){
            return new Result(203,"操作失败，稍后重试");
        }
        return new Result(204,"删除成功√");
    }

    //通过cityId查询城市信息
    @Override
    public Result findCityService(Long cityId) {
        if(cityId==null){
            return new Result(201,"指定的城市数据为空");
        }
        QueryWrapper<City> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("city_id",cityId);
        City city = cityMapper.selectOne(queryWrapper);
        if (city==null){
            return new Result(202,"该城市不存在");
        }
        return new Result(200,"查询成功√",city);
    }
}
